package screens 
{
	import assets.constants.AssetClassNames;
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;
	import events.NavigationEvent;
	import screens.positions.constants.Positions;
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	
	/**
	 * Th splash screen class
	 * @author Epixcs
	 */
	
	public class Splash extends Sprite 
	{
		private var m_bg:Image 			= null; // The background image
		private var m_btnPlay:Button	= null; // The play button
		private var m_btnAbout:Button	= null; // The about button
		
		/*************************************************************************/
		
		/**
		 * The splash screen constructor
		 */
		public function Splash() 
		{
			// super it
			super();
			
			// listen for stage adding
			this.addEventListener( Event.ADDED_TO_STAGE, this._onAddedToStage );
		}
		
		/**
		 * Initialize the splash screen
		 */
		public function initialize():void
		{
			// set splash screen visible
			this.visible = true;
			
			// reset btns positions
			this.m_btnPlay.x	= Positions.BTN_PLAY_START.x;
			this.m_btnPlay.y	= Positions.BTN_PLAY_START.y;
			
			this.m_btnAbout.x	= Positions.BTN_ABOUT_START.x;
			this.m_btnAbout.y	= Positions.BTN_ABOUT_START.y;
			
			// tween them
			Tweener.removeTweens( this.m_btnPlay );
			Tweener.addTween( this.m_btnPlay, { x : Positions.BTN_PLAY_END.x,
												y : Positions.BTN_PLAY_END.y,
												time: Positions.BTN_PLAY_TIME,
												transition: Equations.easeOutSine } );
												
			Tweener.removeTweens( this.m_btnAbout );
			Tweener.addTween( this.m_btnAbout, { x : Positions.BTN_ABOUT_END.x,
												 y : Positions.BTN_ABOUT_END.y,
												 time: Positions.BTN_ABOUT_TIME,
												 delay: Positions.BTN_ABOUT_DELAY, 
												 transition: Equations.easeOutSine } );								
		}
		
		/**
		 * Dispose temporarily the splash screen
		 */
		public function disposeTemporarily():void 
		{
			// set invisible
			this.visible = false;
		}
		
		/*************************************************************************/
		
		// Called when the spash screen is added to stage
		private function _onAddedToStage( e:Event ):void 
		{
			// remove event listener
			this.removeEventListener( Event.ADDED_TO_STAGE, this._onAddedToStage );
			
			// init the splash screen
			this._draw();
		}
		
		// Called to draw the splash screen
		private function _draw():void
		{
			// create the background
			this.m_bg = new Image( Assets.getTexture( AssetClassNames.BG_SPLASH ) );
			
			// create the play button
			this.m_btnPlay = new Button( Assets.getTexture( AssetClassNames.BTN_PLAY ) );
			
			// create the about button
			this.m_btnAbout = new Button( Assets.getTexture( AssetClassNames.BTN_ABOUT ) );
			
			// add all to stage
			this.addChild( this.m_bg );
			this.addChild( this.m_btnPlay );
			this.addChild( this.m_btnAbout );
			
			// reset
			this.initialize();
			
			// add trigger event listener
			this.addEventListener( Event.TRIGGERED, this._onButtonClicked );
		}
		
		// Called when a button has been clicked on
		private function _onButtonClicked( e:Event ):void 
		{
			// get clickec button
			var clickedButton:Button = e.target as Button;
			
			// check if it's the play button
			if ( clickedButton == this.m_btnPlay )
			{
				trace( "PLAY" );
				this.dispatchEvent( new NavigationEvent( NavigationEvent.CHANGE_SCREEN, true, { id: "play" } ) );
				return;
			}
			
			// check if it's the about button
			if ( clickedButton == this.m_btnAbout )
			{
				trace( "ABOUT" );
				return;
			}
			
		}
		
	}

}